<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="/js/jquery-2.1.1.min.js"></script>
    <script src="/js/jquery-form.js"></script>

    <link rel="stylesheet" href="/js/easyui1.5/themes/icon.css">
    <link rel="stylesheet" href="/js/easyui1.5/themes/default/easyui.css">
    <script src="/js/easyui1.5/jquery.easyui.min.js"></script>
</head>
<body>
   <div>
       <form id="query_book_form">

           作者:<select id="cc" class="easyui-combobox" name="authorid" style="width:200px;">
               </select><br>
           标题:<input name="biaoti"class="easyui-textbox" data-options="iconCls:'icon-man'" style="width:200px"><br>
       </form>

       <a id="btn" onclick="queryBook()" href="javascript:void(0)"class="easyui-linkbutton" data-options="iconCls:'icon-remove'">查找</a>
       <a id="btn_add" onclick="addData()" href="javascript:void(0)"class="easyui-linkbutton" data-options="iconCls:'icon-remove'">增加</a>
   </div>
   <table id="dg"></table>

   <!-- 弹框增加start -->
   <div id="dialog_add" class="easyui-dialog" title="My Dialog"
        style="width:800px;height:400px;"
        data-options="iconCls:'icon-save',resizable:true,modal:true,
				buttons:[{
					text:'保存',
					iconCls:'icon-no',
					handler:function(){
						submitData()
						<!--//调用另一个方法-->
					}
				},{
					text:'关闭',
					iconCls:'icon-more',
					handler:function(){

						 $('#dialog_add').dialog('close');
					}
				}]">
       <form id="add_form">
           <input type="hidden" name="id">
           标题:<input name="biaoti"class="easyui-textbox" data-options="iconCls:'icon-man'" style="width:200px"><br>

           作者:<select id="cc1" class="easyui-combobox" name="authorid" style="width:200px;">
               </select><br>

           评论人:<input name="pingname"class="easyui-textbox" data-options="iconCls:'icon-man'" style="width:200px"><br>

           评论内容:<input name="pingtext"class="easyui-textbox" data-options="iconCls:'icon-man'" style="width:200px"><br>
       </form>
   </div>
</body>
<script type="text/javascript" th:inline="none">
    $(function(){
        getPingLun();
        getAllPressData1();
        getAllPressData();

        normal2ajaxFrom();

        $('#dialog_add').dialog('close');
    })
    function getPingLun(){
        $("#dg").datagrid({
            url:'/pl/getPingLun',
            pagination:true,
            //  toolbar:"#gongneng",
            pageList:[1,2,3,4,5,7,10,50],
            pageSize:50,
            checkOnSelect:false,
            columns:[[
                {field:'xxx',title:'xx',width:100,checkbox:true},

                {field:'id',title:'id',width:100},

                {field:'biaoti',title:'标题',width:100},

                {field:'author',title:'作者',width:100},

                {field:'pingname',title:'评论人名称',width:100},

                {field:'pingtext',title:'评论内容',width:100,
                    formatter:function(value,row,index) {

                    return value;
                    }
                },

                {field:'createtime',title:'评论时间',width:100},

                {field:'xsxs',title:'操作',width:100,
                    formatter:function(value,row,index){
                        var content = " <a href='JavaScript:delRole("+row.id+")' >删除</a> ";
                        content += " <a href='JavaScript:updaRole("+row.id+")' >修改</a> ";
                        return content;
                    }
                }
            ]]

        })
    }

    //===修该
     function updaRole(id){
         $.ajax({
             url:"/pl/updaPingLun",
             type:"POST",
             data:{id:id},
             success:function(result){
                 if(result.length>20){

                     window.location="/view/reject"
                 }

                 $("#dialog_add").dialog('open');

                 //1.加载所有出版社数据
                 getAllPressData();

                 $('#add_form').form('load',result);

                 $("#cc").combobox('setValue',result.aid)
             },
             error:function(){
                 alert("ajax访问失败了,查看控制台");
             }
         });
     }


    function getAllPressData1(){
        $('#cc1').combobox({
            url:'/pl/getAuthor',
            valueField:'aid',//<select> <option value='obj.id' > obj.name</select>    $([],functtion(index,obj){})
            textField:'aname'  //<select> <option > xxx  </option> </select>
        });
    }
  //=====作者
    function getAllPressData(){
        $('#cc').combobox({
            url:'/pl/getAuthor',
            valueField:'aid',//<select> <option value='obj.id' > obj.name</select>    $([],functtion(index,obj){})
            textField:'aname'  //<select> <option > xxx  </option> </select>
        });
    }

//====调查
    function queryBook(){

//加载数据  直接发送一个对象
        $('#dg').datagrid('load',serializeFrom2Obj($("#query_book_form")));

    }

    //把表单转换为一个对象
    function serializeFrom2Obj (obj) {
        var o = {};
        var a = obj.serializeArray();//[{name:'price',value='12'},{},{}]
        $.each(a, function() {
            if (o[this.name] !== undefined) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };
//====新增

    function submitData(){//进行提交表单

        $("#add_form").form('submit')//调用表单控件 此时的控件已经是一个easui的控件

    }

    function addData() {
        $("#add_form").form('clear');
        $("#dialog_add").dialog('open')
    }

    function normal2ajaxFrom(){
        //使普通表单成为ajax提交方式的表单。
        $('#add_form').form({

            url:"/pl/addPingLun",
            onSubmit: function(){
            },
            success:function(data){
                if(data.length>20){

                    window.location="/view/reject"
                }//在表单提交成功以后触发 data是后台返回的数据
                //关闭对话框
                $("#dialog_add").dialog('close');
                //重新加载数据
                $("#dg").datagrid('reload');//reload 会重新请求数据 但会停留在当前页
            }
        });
    }
//====删除
    function delRole(id){
        $.ajax({
            url:"/pl/delPingLun",
            type:"POST",
            data:{id:id},
            success:function(result){
                if(result.length>20){

                    window.location="/view/reject"
                }
                //数据库已经删除成功 页面需要更新
                $("#dg").datagrid('reload');//reload 会重新请求数据 但会停留在当前页
                //load 会跑到第一页
                $.messager.show({
                    title:'消息',
                    msg:'删除成功!!',
                    timeout:5000,
                    showType:'slide'
                });

            },
            error:function(){
                alert("ajax访问失败了,查看控制台");
            }
        });

    }
</script>
</html>